package com.example.demo.day0305;
import java.lang.reflect.*;
//@TestAnnotation(age = 12,name = "")
//@TestAnnotation(value = 12,ta1 = @TestAnnotation1,str = {"a","b"})
//@MyAnnotation
//@DemoAnnotation(className = "com.example.demo.day0305.Demo1",methodName = "show")
@DemoAnnotation(className = "com.example.demo.day0305.Demo2",methodName = "show")
public class Test {
//    @MyAnnotation
//    public String name = "tom";
//    @MyAnnotation//加到方法上会报错
//@MyAnnotation
//public void show(){}
public static void main(String[] args) {
    //解析注解
    Class<Test> t = Test.class;
    //获取该类的字节码文件对象

    DemoAnnotation dn = t.getAnnotation(DemoAnnotation.class);//.获取上边的注解对象,就是在内存中生成了一个该注解接口的子类实现对象
    //调用注解对象中定义的抽象方法，获取返回值
    String className = dn.className();
    String methodName = dn.methodName();
    System.out.println(className);//得到注解配置的两个属性值
    System.out.println(methodName);

    try {
        Class c = Class.forName(className);//获取Class实例
        Object obj = c.newInstance();//创建新对象
        Method method = c.getMethod(methodName);//获取方法
        method.invoke(obj);//通过invoke调用方法

    } catch (Exception e) {
        e.printStackTrace();
    }

}
}
